<?php
/**
 * Created by PhpStorm.
 * 商家设置
 * User: caicm
 * Date: 2017/6/29
 * Time: 14:40
 */

namespace WMApi\Controller;
use WMApi\Model\WmAliyunPushModel;
use WMApi\Service\AliyunPushService;
use WMApi\Service\SellerSetService;
use WMApi\Model\UsersModel;
use WMApi\Model\WmSellerModel;

class SellerSetController extends SellerBaseController {

    /**
     *发送验证码
     * Created By caicm
     * @param int phone 手机号
     * @param int type 验证码类型 1-设置提现密码 2-设置登陆密码
     */
    public function send_verificode(){
        $user_phone = I("post.phone");
        $type = I('post.type', 0, 'int');

        if(empty($user_phone) || !is_mobile($user_phone) || empty($type)){
            returnApiError("参数有误");
        }

        $user_model = new UsersModel();
        $seller_info = $user_model->find_users_seller_info($this->seller_id);
        if($seller_info['mobile_phone'] != $user_phone){
            returnApiError('手机号码不正确');
        }

        $ver_code = getSmsCode();
        $send_result = A("SDXApi/Smsapi")->send_message_api("ver_code", $user_phone, 0, 0, 0, $ver_code, 0);
        if($send_result){
            S("seller_set_".$user_phone."_code",$ver_code,600);    //验证码保持600秒
            returnApiSuccess(null,'发送验证码成功');
        }else{
            returnApiError('发送验证码失败');
        }

    }

    /**
     * 设置提现密码
     * Created By caicm
     * @param int phone 手机号
     * @param string withdraw_password 密码
     * @param int verifi_code 验证码
     */
    public function set_withdraw_password(){
        $withdraw_password = I('post.withdraw_password');
        $user_phone = I("post.phone");
        $verifi_code = I('post.verifi_code');
        if(empty($user_phone) || !is_mobile($user_phone) || empty($withdraw_password) || empty($verifi_code)){
            returnApiError("参数有误");
        }

        $this->check_verifi_code($user_phone, $verifi_code);

        $user_model = new UsersModel();
        $seller_model = new WmSellerModel();

        $seller_info = $user_model->find_users_seller_info($this->seller_id);
        if($seller_info['mobile_phone'] != $user_phone){
            returnApiError('手机号码不正确');
        }

        $set_service = new SellerSetService();
        $withdraw_password = $set_service->compile_withdraw_password($withdraw_password);

        $res = $seller_model->update_withdraw_password($this->seller_id, $withdraw_password);
        if($res){
            //清空验证码
            S("seller_set_".$user_phone."_code",null);
            returnApiSuccess(null, '操作成功！');
        }else{
            returnApiError('操作失败！');
        }
    }

    /**
     * 验证验证码
     * @param int $user_phone
     * @param string $verifi_code
     * @return bool
     */
    protected function check_verifi_code($user_phone=0, $verifi_code=''){
        if(S("seller_set_".$user_phone."_code") != $verifi_code){
            returnApiError("验证码不正确");
        }
        return true;
    }

    /**
     * 修改阿里云推送app设备id
     * Created By caicm
     * @param int seller_id 商家id
     * @param string device_id 设备id
     */
    public function update_aliyun_push(){
        $device_id = I('post.device_id');
        if(empty($device_id)){
            returnApiError('参数有误');
        }

        $source = get_source();
        $android_device_id = '';
        $ios_device_id = '';
        if ($source == 'android'){
            $android_device_id = $device_id;
            $ios_device_id = '';

        }elseif ($source == 'ios'){
            $android_device_id = '';
            $ios_device_id = $device_id;

        }else{
            returnApiError('参数有误');
        }

        $push_model = new WmAliyunPushModel();
        $push_info = $push_model->find_push_info($this->seller_id);
        if($push_info){
            $res = $push_model->update_push($this->seller_id, $android_device_id, $ios_device_id);
        }else{
            $res = $push_model->add_push($this->seller_id, $android_device_id, $ios_device_id);
        }

        if($res){
            returnApiSuccess(null, '操作成功');
        }else{
            returnApiError('操作失败');
        }
    }

    /**
     * 修改商家绑定手机号码
     * Created By caicm
     * @param int phone 新手机号码
     * @param int verifi_code 验证码
     * @param string password 登录密码
     */
    public function update_phone(){
        $phone = I("post.phone");
        $verifi_code = I('post.verifi_code');
        $password = I('post.password');
        if(empty($phone)){
            returnApiError("手机号码不能为空");
        }
        if(!is_mobile($phone)){
            returnApiError("请输入正确的手机号");
        }
        if(empty($verifi_code)){
            returnApiError("验证码不能为空");
        }
        if(S("seller_".$phone."_code") != $verifi_code){
            returnApiError("验证码不正确");
        }
        if(empty($password)){
            returnApiError("密码不能为空");
        }

        $users_model = new UsersModel();

        $seller_info = $users_model->find_users_seller_info($this->seller_id);

        //密码加密
        $user_password = compile_password(array('password'=>$password, 'ec_salt'=>$seller_info['ec_salt']));
        if($seller_info['password'] !=$user_password){
            returnApiError("密码有误！");
        }

        if($users_model->get_users_conut_by_phone($phone)){
            returnApiError('该手机已被注册');
        }

        $state = $users_model->update_phone($seller_info['user_id'], $phone);

        if($state){
            //清空验证码
            S("seller_".$phone."_code",null);
            returnApiSuccess(null, '操作成功！');
        }else{
            returnApiError('操作失败！');
        }

    }

    /**
     * 阿里云app推送
     * Created By caicm
     */
    public function aliyun_push(){
        $title = '接口测试';
        $body = '内容';

        $aliyun_push = new AliyunPushService();
        $message_id = $aliyun_push->app_push($this->seller_id, $title, $body);

        returnApiSuccess(array('message_id' => $message_id));
    }

    /**
     * 阿里云app推送-查询
     * Created By caicm
     */
    public function query_push_state(){
        $message_id = I('post.message_id');
        if(empty($message_id)){
            returnApiError('参数有误');
        }

        $aliyun_push = new AliyunPushService();
        $res = $aliyun_push->query_push_state($message_id);

        print_r($res);exit;
        returnApiSuccess($res);
    }

    public function post_to_ecth(){
        $data = array(
            'longitude' => 123,
            'latitude' => 456,
        );

        $state = postToEchouchApi('WmAip','test', $data);

        print_r($state);exit;
    }
}